home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993 April: Penguin on DISC / ADC Developer CD (1993-04) (''Penguin On DISC'')_iso / Dev.CD Apr 93.iso / System Software / System Extensions / AppleScript Beta / Development Tools / Sample Applications / 7Edit 3.0d7 / 7Edit Source / 7Edit C 3.0d7 / SVEditMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-24  |  16.0 KB  |  652 lines  |  [TEXT/MPS ]

  1. /*
  2.     SVEditMain.c
  3.     
  4.     Version 3.0d7
  5.     
  6.     Copyright © Apple Computer UK Ltd. 1991,1992
  7.     
  8.     All rights reserved.
  9.     
  10.     Produced by : UK Developer Technical Support
  11.     AppleLink   : UK.DTS
  12.  
  13. */
  14. /** This example is brought to you for the purposes of exploration and experimentation of
  15. System 7.0.  It is not intended to form the basis of your own programs- but try out the code-
  16. that's what it's there for **/
  17.  
  18. /*System 7.0 specifics-:
  19.  
  20. We check the configuration on startup to see if we are running on a pre-7.0
  21. machine.    Much as we'd like to handle both environments in 7Edit, we thought
  22. exiting gracefully under pre-7.0 systems was the best solution.
  23.  
  24.  
  25. Notice the additions to the main event loop to support section events and the
  26. despatching of AppleEvents.
  27.  
  28.     New for 3.0d2 :
  29.     
  30.     21-Feb-92 : NH : Fix type into subscribers
  31.     26-Feb-92 : NH : ShowBorders grey when no window
  32.     27-Feb-92 : NH : Test menu and gCurrSection zapped
  33.      5-Mar-92 : NH : Create Publisher - event driven (ish)
  34.     27-Mar-92 : NH : Update menu status before MenuKey
  35.     
  36.     Changes for 3.0d5 :
  37.     
  38.     11-Aug-92 : NH : Quit via AppleEvent
  39. */
  40.  
  41. #include <Memory.h>
  42. #include <QuickDraw.h>
  43. #include <Types.h>
  44. #include <Menus.h>
  45. #include <Windows.h>
  46. #include <Dialogs.h>
  47. #include <Traps.h>
  48. #include <Packages.h>
  49. #include <PPCToolbox.h>
  50. #include <Editions.h>
  51. #include <Printing.h>
  52. #include <ToolUtils.h>
  53. #include <Desk.h>
  54. #include <Scrap.h>
  55. #include <OsEvents.h>
  56. #include <AppleEvents.h>
  57. #include <AEObjects.h>
  58. #include <Errors.h>
  59.  
  60. #include "SVEditGlobals.h"
  61. #include "SVEditUtils.h"
  62. #include "SVEditions.h"
  63. #include "SVAppleEvents.h"
  64. #include "SVEditWindow.h"
  65. #include "SVEditFile.h"
  66.  
  67. /*-----------------------------------------------------------------------*/
  68. /**----------                         Standard Main routines                  --------------**/
  69. /*-----------------------------------------------------------------------*/
  70.  
  71. #pragma segment Main
  72.  
  73.     pascal void MaintainCursor(void)
  74.         {
  75.             Point     pt;
  76.             WindowPtr wPtr;
  77.             GrafPtr   savePort;
  78.             DPtr      theDoc;
  79.  
  80.             wPtr = FrontWindow();
  81.             if (Ours(wPtr))
  82.                 {
  83.                     theDoc = DPtrFromWindowPtr(wPtr);
  84.                     GetPort(&savePort);
  85.                     SetPort(wPtr);
  86.                     GetMouse(&pt);
  87.                     if (theDoc->theText)
  88.                         if (PtInRect(pt, &(**(theDoc->theText)).viewRect))
  89.                             SetCursor(&editCursor);
  90.                         else
  91.                             SetCursor(&qd.arrow);
  92.                     else
  93.                         SetCursor(&qd.arrow);
  94.  
  95.                     if (theDoc->theText)
  96.                         TEIdle(theDoc->theText);
  97.  
  98.                     SetPort(savePort);
  99.                 }
  100.         }
  101.  
  102. #pragma segment Main
  103.  
  104. pascal void MaintainMenus(void)
  105.     {
  106.         DPtr       theDoc;
  107.         WindowPtr  firstWindow;
  108.         SectHandle currSection;
  109.  
  110.         firstWindow = FrontWindow();
  111.         if (!Ours(firstWindow))
  112.             {
  113.                 EnableItem(myMenus[fileM], fmNew);
  114.                 EnableItem(myMenus[fileM], fmOpen);
  115.                 DisableItem(myMenus[fileM], fmClose);
  116.                 DisableItem(myMenus[fileM], fmSave);
  117.                 DisableItem(myMenus[fileM], fmSaveAs);
  118.                 DisableItem(myMenus[fileM], fmRevert);
  119.                 DisableItem(myMenus[fileM], fmPrint);
  120.                 DisableItem(myMenus[fileM], fmPageSetUp);
  121.                 
  122.                 DisableItem(myMenus[editM],  cPublisher);
  123.                 DisableItem(myMenus[editM],  cSubscriber);
  124.                 DisableItem(myMenus[editM],  cOptions);
  125.                 DisableItem(myMenus[editM],  cBorders);
  126.                 
  127.                 if (firstWindow)
  128.                     {
  129.                         EnableItem(myMenus[editM], undoCommand);
  130.                         EnableItem(myMenus[editM], cutCommand);
  131.                         EnableItem(myMenus[editM], copyCommand);
  132.                         EnableItem(myMenus[editM], pasteCommand);
  133.                         EnableItem(myMenus[editM], clearCommand);
  134.                     }
  135.             }
  136.         else
  137.             {
  138.                 theDoc = DPtrFromWindowPtr(firstWindow);
  139.                 EnableItem(myMenus[editM], pasteCommand);
  140.                 EnableItem(myMenus[editM], cBorders);
  141.                 EnableItem(myMenus[fileM], fmClose);
  142.                 EnableItem(myMenus[fileM], fmSaveAs);
  143.                 EnableItem(myMenus[fileM], fmPrint);
  144.                 EnableItem(myMenus[fileM], fmPageSetUp);
  145.  
  146.                 if (theDoc->dirty)
  147.                     EnableItem(myMenus[fileM], fmRevert);
  148.                 else
  149.                     DisableItem(myMenus[fileM], fmRevert);
  150.                     
  151.                 if ((theDoc->dirty) && (theDoc->everSaved))
  152.                     EnableItem(myMenus[fileM], fmSave);
  153.                 else
  154.                     DisableItem(myMenus[fileM], fmSave);
  155.  
  156.                 DisableItem(myMenus[editM], undoCommand);
  157.                 
  158.                 if (((**(theDoc->theText)).selEnd - (**(theDoc->theText)).selStart) < 0)
  159.                     {
  160.                         DisableItem(myMenus[editM], cutCommand);
  161.                         DisableItem(myMenus[editM], copyCommand);
  162.                         DisableItem(myMenus[editM], clearCommand);
  163.                         DisableItem(myMenus[editM], cPublisher);
  164.                     }
  165.                 else
  166.                     {
  167.                         EnableItem(myMenus[editM], cutCommand);
  168.                         EnableItem(myMenus[editM], copyCommand);
  169.                         EnableItem(myMenus[editM], clearCommand);
  170.                         EnableItem(myMenus[editM], cPublisher);
  171.                     }
  172.  
  173.                 currSection = GetSection((**(theDoc->theText)).selStart,
  174.                                                                  (**(theDoc->theText)).selEnd,
  175.                                                                  theDoc);
  176.                 if (currSection)
  177.                     {
  178.                         DisableItem(myMenus[editM], cPublisher);
  179.                         DisableItem(myMenus[editM], cSubscriber);
  180.                         EnableItem(myMenus[editM],  cOptions);
  181.                         if ((**(**currSection).fSectHandle).kind == stPublisher)
  182.                             SetItem(myMenus[editM], cOptions, "\pPublisher Options…");
  183.                         else
  184.                             SetItem(myMenus[editM], cOptions, "\pSubscriber Options…");
  185.                     }
  186.                 else
  187.                     {
  188.                         EnableItem(myMenus[editM],  cPublisher);
  189.                         EnableItem(myMenus[editM],  cSubscriber);
  190.                         DisableItem(myMenus[editM], cOptions);
  191.                     }
  192.             }
  193.     }
  194.  
  195. #pragma segment Main
  196.  
  197. pascal void SetUpCursors(void)
  198.  
  199.     {
  200.         CursHandle  hCurs;
  201.  
  202.         hCurs = GetCursor(1);
  203.         editCursor = **hCurs;
  204.         hCurs = GetCursor(watchCursor);
  205.         waitCursor = **hCurs;
  206.     }
  207.  
  208. #pragma segment Main
  209.  
  210. pascal void SetUpMenus(void)
  211.     {
  212.         short i;
  213.         
  214.         myMenus[appleM] = GetMenu(appleID);
  215.         AddResMenu(myMenus[appleM], 'DRVR');
  216.         myMenus[fileM] = GetMenu(fileID);
  217.         myMenus[editM] = GetMenu(editID);
  218.         myMenus[fontM] = GetMenu(mfontID);
  219.         AddResMenu(myMenus[fontM], 'FONT');
  220.         myMenus[sizeM]  = GetMenu(sizeID);
  221.         myMenus[styleM] = GetMenu(styleID);
  222.  
  223.         for (i = appleM; i <= kLastMenu; i++)
  224.             InsertMenu(myMenus[i], 0);
  225.  
  226.         SetItemStyle(myMenus[styleM], cPlain, 0);
  227.         SetItemStyle(myMenus[styleM], cBold, bold);
  228.         SetItemStyle(myMenus[styleM], cItalic, italic);
  229.         SetItemStyle(myMenus[styleM], cUnderline, underline);
  230.         SetItemStyle(myMenus[styleM], cOutline, outline);
  231.         SetItemStyle(myMenus[styleM], cShadow, shadow);
  232.         SetItemStyle(myMenus[styleM], cCondense, condense);
  233.         SetItemStyle(myMenus[styleM], cExtend, extend);
  234.  
  235.         SetShortMenus(); /* Does a DrawMenuBar() */
  236.     }
  237.  
  238. pascal void DoFile(short theItem)
  239.     {        
  240.         short   alertResult;
  241.         DPtr    theDoc;
  242.         FSSpec  theFSSpec;
  243.         OSErr   fileErr;
  244.         TPrint  thePSetup;
  245.  
  246.         switch (theItem){
  247.             case fmNew : IssueAENewWindow();
  248.                    break;
  249.             case fmOpen: if (GetFile(&theFSSpec)==noErr)
  250.                                          fileErr = IssueAEOpenDoc(theFSSpec);
  251.                    break;
  252.             case fmClose:IssueCloseCommand(FrontWindow());
  253.                    break;
  254.             case fmSave:
  255.             case fmSaveAs:
  256.                                         theDoc = DPtrFromWindowPtr(FrontWindow());
  257.                     
  258.                                         if (theDoc->everSaved == false || theItem==fmSaveAs)
  259.                                             {
  260.                                                 fileErr = GetFileNameToSaveAs(theDoc);
  261.                                                 if (fileErr!=noErr && fileErr!=userCanceledErr)
  262.                                                     FileError("\perror saving ", theDoc->theFileName);
  263.                                                 else
  264.                                                     fileErr = IssueSaveCommand(theDoc->theWindow, &theDoc->theFSSpec);
  265.                 
  266.                                             if (fileErr == noErr)
  267.                                                 SetWTitle(theDoc->theWindow, theDoc->theFSSpec.name);
  268.                                             }
  269.                                         else
  270.                                             fileErr = IssueSaveCommand(theDoc->theWindow, nil);
  271.                                         break;
  272.  
  273.             case fmRevert:SetCursor(&qd.arrow);
  274.                                         theDoc = DPtrFromWindowPtr(FrontWindow());
  275.                     
  276.                                         ParamText("\pRevert to the last saved version of ", theDoc->theFileName, "", "");
  277.                                         alertResult = Alert(AdviseAlert, nil);
  278.                                         switch (alertResult){
  279.                                             case aaSave:if (IssueRevertCommand(theDoc->theWindow))
  280.                                                                         FileError("\perror reverting ", theDoc->theFileName);
  281.                                         }
  282.                                         break;
  283.  
  284.             case fmPageSetUp:
  285.                              theDoc = DPtrFromWindowPtr(FrontWindow());
  286.                                              if (DoPageSetup(theDoc))
  287.                                                  {
  288.                                                      thePSetup = **(theDoc->thePrintSetup);
  289.                                                      IssuePageSetupWindow(theDoc->theWindow, thePSetup);
  290.                                                  }
  291.                                            break;
  292.  
  293.             case fmPrint: IssuePrintWindow(FrontWindow());
  294.                                       break;
  295.  
  296.             case fmQuit : IssueQuitCommand();
  297.                     break;
  298.         }                 /*of switch*/
  299.     }
  300.  
  301. #pragma segment Main
  302.  
  303. pascal void DoCommand(long mResult)
  304.     {
  305.         short   theItem;
  306.         short   err;
  307.         long    result;
  308.         Str255  name;
  309.         DPtr    theDocument;
  310.  
  311.         theDocument = DPtrFromWindowPtr(FrontWindow());
  312.         
  313.         theItem = LoWord(mResult);
  314.  
  315.         switch (HiWord(mResult)){
  316.  
  317.             case appleID:
  318.                 if (theItem == aboutItem)
  319.                     {
  320.                         SetCursor(&qd.arrow);
  321.                         result = Alert(258, nil);
  322.                     }
  323.                 else
  324.                     {
  325.                         GetItem(myMenus[appleM], theItem, name);
  326.                         err = OpenDeskAcc(name);
  327.                         SetPort(FrontWindow());
  328.                     }
  329.         break;
  330.                 
  331.             case fileID: DoFile(theItem);
  332.                          break;
  333.  
  334.             case editID:
  335.                     if (SystemEdit(theItem - 1) == false);
  336.  
  337.                     switch (theItem){
  338.  
  339.                         case cutCommand   : IssueCutCommand(theDocument);
  340.                            break;
  341.                                                      
  342.                         case copyCommand  : IssueCopyCommand(theDocument);
  343.                            break;
  344.                                                      
  345.                         case pasteCommand : IssuePasteCommand(theDocument);
  346.                            break;
  347.                                                      
  348.                         case clearCommand : IssueClearCommand(theDocument);
  349.                            break;
  350.                                                      
  351.                         case selectAllCommand :
  352.                                                      if (theDocument)
  353.                                                          TESetSelect(0,
  354.                                                                                  (**(theDocument->theText)).teLength,
  355.                                                                                  theDocument->theText);
  356.                            break;
  357.                                                      
  358.                         case cPublisher : IssueCreatePublisher(theDocument);
  359.                                                  break;
  360.                                                      
  361.                         case cSubscriber: DoSubscribe(theDocument);
  362.                                                  break;
  363.                                                      
  364.                         case cOptions   : DoSectionOptions(theDocument);
  365.                                                  break;
  366.                                                      
  367.                         case cBorders   : IssueShowBorders(theDocument->theWindow,
  368.                                                       !theDocument->showBorders);
  369.                                                  break;
  370.                                                      
  371.                     }     /*of switch*/
  372.                     ShowSelect(theDocument);
  373.           break;
  374.  
  375.             case mfontID: IssueFontCommand(theDocument, theItem);
  376.                             break;
  377.  
  378.             case sizeID: IssueSizeCommand(theDocument, theItem);
  379.                             break;
  380.  
  381.             case styleID: IssueStyleCommand(theDocument, theItem);
  382.                              break;
  383.  
  384.         }                 /*of switch*/
  385.  
  386.         HiliteMenu(0);
  387.  
  388.     }
  389.  
  390. #pragma segment Main
  391.  
  392.  
  393.     pascal void DoMouseDown(const EventRecord *myEvent)
  394.         {
  395.             WindowPtr whichWindow;
  396.             Point     p;
  397.             Rect      dragRect;
  398.             DPtr      theDoc;
  399.  
  400.             p = myEvent->where;
  401.             switch (FindWindow(p, &whichWindow)){
  402.  
  403.                 case inDesk: SysBeep(10);
  404.                              break;
  405.  
  406.                 case inGoAway:if (Ours(whichWindow))
  407.                                                 if (TrackGoAway(whichWindow, p))
  408.                                                     IssueCloseCommand(whichWindow);
  409.                                             break;
  410.                 case inMenuBar:
  411.                                             SetCursor(&qd.arrow);
  412.                                             theDoc = DPtrFromWindowPtr(FrontWindow());
  413.                                             if (theDoc)
  414.                                                 {
  415.                                                     SetFontMenu(theDoc);
  416.                                                     SetEditMenu(theDoc);
  417.                                                 }
  418.                     
  419.                                             DoCommand(MenuSelect(p));
  420.                                             HiliteMenu(0);
  421.                                             break;
  422.  
  423.                 case inSysWindow: SystemClick(myEvent, whichWindow);
  424.                                   break;
  425.  
  426.                 case inDrag:
  427.                     
  428.                         dragRect = qd.screenBits.bounds;
  429.                         
  430.                         if (Ours(whichWindow))
  431.                             {
  432.                                 DragWindow(whichWindow, p, &dragRect);
  433.                                 /*
  434.                                     As rgnBBox may be passed by address
  435.                                 */
  436.                                 dragRect = (**((WindowPeek)whichWindow)->strucRgn).rgnBBox;
  437.                                 /*
  438.                                     The windows already there, but still tell
  439.                                     the our AppleEvents core about the move in case
  440.                                     they want to do anything
  441.                                 */
  442.                                 IssueMoveWindow(whichWindow, dragRect);
  443.                             }
  444.                       break;
  445.  
  446.                 case inGrow:SetCursor(&qd.arrow);
  447.                                         if (Ours(whichWindow))
  448.                                             MyGrowWindow(whichWindow, p);
  449.                                         break;
  450.                                     
  451.                 case inZoomIn : DoZoom(whichWindow, inZoomIn, p);
  452.                                               break;
  453.  
  454.                 case inZoomOut: DoZoom(whichWindow, inZoomOut, p);
  455.                         break;
  456.                 case inContent: if (whichWindow != FrontWindow())
  457.                                                     SelectWindow(whichWindow);
  458.                                                 else
  459.                                                     if (Ours(whichWindow))
  460.                                                         DoContent(whichWindow, *myEvent);
  461.                         break;
  462.             }                 /*of switch*/
  463.         }
  464.  
  465. #pragma segment Main
  466.  
  467. pascal long GetSleep(void)
  468.     {
  469.         long      sleep;
  470.         WindowPtr theWindow;
  471.         DPtr      theDoc;
  472.  
  473.         sleep = 0x7fffffff;
  474.         if (!gInBackground)
  475.             {
  476.                 theWindow = FrontWindow();
  477.                 if (theWindow)
  478.                     {
  479.                         theDoc = DPtrFromWindowPtr(theWindow);
  480.                         if ((**(theDoc->theText)).selStart == (**(theDoc->theText)).selEnd)
  481.                             sleep = GetCaretTime();
  482.                     }
  483.             }
  484.         return(sleep);
  485.     }                     /*GetSleep*/
  486.  
  487. #pragma segment Main
  488.  
  489.     
  490. pascal void MainEvent(void)
  491.     {
  492.         DPtr        theDoc;
  493.         EventRecord myEvent;
  494.         char        theChar;
  495.         WindowPtr   theWindow;
  496.         Boolean     activate;
  497.         Boolean     keyIsOk;
  498.         SectHandle  currSection;
  499.         
  500.  
  501.         MaintainCursor(); /* TEIdle in here for now */
  502.         MaintainMenus();
  503.  
  504.         if (WaitNextEvent(everyEvent, &myEvent, GetSleep(), nil))
  505.             {
  506.                 switch (myEvent.what) {
  507.                     case mouseDown: FlushAndRecordTypingBuffer();
  508.                                                     DoMouseDown(&myEvent);
  509.                                     break;
  510.                     case keyDown:
  511.                     case autoKey:
  512.                                 theDoc = DPtrFromWindowPtr(FrontWindow());
  513.     
  514.                                 theChar = myEvent.message & charCodeMask;
  515.     
  516.                                 if ((myEvent.modifiers & cmdKey) == cmdKey)
  517.                                     {
  518.                                         DoCommand(MenuKey(theChar));
  519.                                         HiliteMenu(0);
  520.                                     }
  521.                                 else
  522.                                     if (theDoc->theText)
  523.                                         {
  524.                                             keyIsOk = true;
  525.                                             /*
  526.                                                 don't allow a subscriber to be changed
  527.                                             */
  528.                                             currSection = GetSection((**(theDoc->theText)).selStart,
  529.                                                                                              (**(theDoc->theText)).selEnd,
  530.                                                                                              theDoc);
  531.                                             
  532.                                             if (currSection)
  533.                                                 if ((**(**currSection).fSectHandle).kind == stSubscriber)
  534.                                                     keyIsOk = KeyOKinSubscriber(theChar);                                                    
  535.                                             
  536.                                             if (keyIsOk)
  537.                                                 {
  538.                                                     DoTEKeySectionRecalc(theDoc, theChar);
  539.                                                     
  540.                                                     AddKeyToTypingBuffer(theDoc, theChar);
  541.                                                     
  542.                                                     TEKey(theChar, theDoc->theText);
  543.                                                     AdjustScrollbars(theDoc, false);
  544.                                     
  545.                                                     ShowSelect(theDoc);
  546.                                                     
  547.                                                     theDoc->dirty = true;
  548.                                                 }
  549.                                         }
  550.                               break;
  551.                         
  552.                     case activateEvt:
  553.                             activate = ((myEvent.modifiers & activeFlag) != 0);
  554.                             theWindow = (WindowPtr)myEvent.message;
  555.                             DoActivate(theWindow, activate);
  556.                           break;
  557.  
  558.                     case updateEvt:
  559.                             theDoc = DPtrFromWindowPtr((WindowPtr)myEvent.message);
  560.                             DoUpdate(theDoc);
  561.                           break;
  562.  
  563.                     case kHighLevelEvent: FlushAndRecordTypingBuffer();
  564.                                                                 DoAppleEvent(myEvent);
  565.                                 break;
  566.                     case kOSEvent:
  567.                         
  568.                         switch (myEvent.message & osEvtMessageMask) { /*high byte of message*/
  569.                             case 0x01000000:
  570.                                 {
  571.                                     gInBackground = ((myEvent.message & resumeFlag) == 0);
  572.                                     DoActivate(FrontWindow(), !gInBackground);
  573.                                 }
  574.                         }
  575.                     }
  576.                 }             /*of switch*/
  577.         }
  578.  
  579. #pragma segment Main
  580.  
  581. pascal void DoSVEdit(void)
  582.     {
  583.         OSErr  err;
  584.         short  result;
  585.  
  586.         InitGraf(&qd.thePort);
  587.         InitFonts();
  588.         FlushEvents(everyEvent, 0);
  589.         InitWindows();
  590.         InitMenus();
  591.         TEInit();
  592.         InitDialogs(nil);
  593.         InitCursor();
  594.  
  595.         MaxApplZone();
  596.         SetUpCursors();
  597.  
  598.         SetUpMenus();
  599.  
  600.         gWCount    = 0;
  601.         gNewDocCount = 0;
  602.         gQuitting  = false;
  603.         gFontMItem = 0;
  604.  
  605.         gGestaltAvailable          = false;
  606.         gAppleEventsImplemented    = false;
  607.         gAliasManagerImplemented   = false;
  608.         gEditionManagerImplemented = false;
  609.         gOutlineFontsImplemented   = false;
  610.  
  611.         /*check environment checks to see if we are running 7.0*/
  612.         if (!CheckEnvironment())
  613.             {
  614.                 SetCursor(&qd.arrow);
  615.                 /*pose the only 7.0 alert*/
  616.                 result = Alert(302, nil);
  617.                 return;
  618.             }
  619.  
  620.         err = InitEditionPack();
  621.         if (err)
  622.             {
  623.                 ShowError("\pInitEditionPack", err);
  624.                 gQuitting = true;
  625.             }
  626.  
  627.         err = AEObjectInit();
  628.         if (err)
  629.             {
  630.                 ShowError("\pAEObjectInit", err);
  631.                 gQuitting = true;
  632.             }
  633.  
  634.         InitAppleEvents();
  635.  
  636.         err = PPCInit();
  637.         if (err)
  638.             {
  639.                 ShowError("\pPPCInit", err);
  640.                 gQuitting = true;
  641.             }
  642.  
  643.         while (!gQuitting)
  644.             MainEvent();
  645.     }
  646.     
  647. main ()
  648.     {
  649.         /*the main routine starts here*/
  650.         DoSVEdit();
  651.     }
  652.